<meta charset="utf-8">
(#) Use the suspending function setCurrentState(), rather than directly accessing the currentState property

!!! ERROR: Use the suspending function setCurrentState(), rather than directly accessing the currentState property
   This is an error.

Id
:   `TestLifecycleOwnerInCoroutine`
Summary
:   Use the suspending function setCurrentState(), rather than directly accessing the currentState property
Severity
:   Error
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Identifier
:   androidx.lifecycle
Feedback
:   https://issuetracker.google.com/issues/new?component=413132
Min
:   Lint 8.0 and 8.1
Compiled
:   Lint 8.7+
Artifact
:   [androidx.lifecycle:lifecycle-runtime-testing](androidx_lifecycle_lifecycle-runtime-testing.md.html)
Since
:   2.7.0
Affects
:   Kotlin and Java files and test sources
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/lifecycle/lifecycle-runtime-testing-lint/src/main/java/androidx/lifecycle/testing/lint/TestLifecycleOwnerInCoroutineDetector.kt)
Tests
:   [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/lifecycle/lifecycle-runtime-testing-lint/src/test/java/androidx/lifecycle/runtime/testing/lint/TestLifecycleOwnerInCoroutineDetectorTest.kt)
Copyright Year
:   2021

When using TestLifecycleOwner, one of the main use cases is to change
             the currentState property. Under the hood, we do this using
runBlocking to keep                 it thread-safe. However, when using
TestLifecycleOwner from the context of a                 coroutine (like
runTest), this will cause the setter to hang, since coroutines
      should remain asynchronous.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/example/foo/test.kt:8:Error: Incorrect use of currentState property
inside of Coroutine, please use the suspending setCurrentState()
function. [TestLifecycleOwnerInCoroutine]
    fun testSetCurrentStateInRunTest() = runTest {
                                         -------
src/example/foo/test.kt:13:Error: Incorrect use of currentState property
inside of Coroutine, please use the suspending setCurrentState()
function. [TestLifecycleOwnerInCoroutine]
    fun testSetCurrentStateInRunTestWithTimeOut() = runTest(timeout = 5000) {
                                                    -------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`src/example/foo/test.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package example.foo

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.testing.TestLifecycleOwner
import kotlinx.coroutines.test.runTest

fun testSetCurrentStateInRunTest() = runTest {
    val owner = TestLifecycleOwner()
    owner.currentState = Lifecycle.State.RESUMED
}

fun testSetCurrentStateInRunTestWithTimeOut() = runTest(timeout = 5000) {
    val owner = TestLifecycleOwner()
    owner.currentState = Lifecycle.State.RESUMED
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/lifecycle/lifecycle-runtime-testing-lint/src/test/java/androidx/lifecycle/runtime/testing/lint/TestLifecycleOwnerInCoroutineDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `TestLifecycleOwnerInCoroutineDetector.errors`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=413132.

(##) Including

!!!
   This is not a built-in check. To include it, add the below dependency
   to your project.

```
// build.gradle.kts
implementation("androidx.lifecycle:lifecycle-runtime-testing:2.9.0-alpha08")

// build.gradle
implementation 'androidx.lifecycle:lifecycle-runtime-testing:2.9.0-alpha08'

// build.gradle.kts with version catalogs:
implementation(libs.lifecycle.runtime.testing)

# libs.versions.toml
[versions]
lifecycle-runtime-testing = "2.9.0-alpha08"
[libraries]
# For clarity and text wrapping purposes the following declaration is
# shown split up across lines, but in TOML it needs to be on a single
# line (see https://github.com/toml-lang/toml/issues/516) so adjust
# when pasting into libs.versions.toml:
lifecycle-runtime-testing = {
    module = "androidx.lifecycle:lifecycle-runtime-testing",
    version.ref = "lifecycle-runtime-testing"
}
```

2.9.0-alpha08 is the version this documentation was generated from;
there may be newer versions available.

[Additional details about androidx.lifecycle:lifecycle-runtime-testing](androidx_lifecycle_lifecycle-runtime-testing.md.html).
(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("TestLifecycleOwnerInCoroutine")
  fun method() {
     runTest(...)
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("TestLifecycleOwnerInCoroutine")
  void method() {
     runTest(...);
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection TestLifecycleOwnerInCoroutine
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="TestLifecycleOwnerInCoroutine" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'TestLifecycleOwnerInCoroutine'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore TestLifecycleOwnerInCoroutine ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>